//查找算法汇总

#define MaxSize 10

struct staticTable{
    int Data[MaxSize]; 
    int Length;
};

typedef struct staticTable * Pst;

//顺序查找
int sequentialSearch(Pst tb, int k){
    //在表tb[1]~tb[n]中查找关键字为k的数据元素
    int i;
    tb->Data[0] = k;
    for(i = tb->Length;tb->Data[i] != k; i--);
    return i;
}

//二分查找
int BinarySearch(Pst tb,int k){
    //在表tb中查找关键字为k的数据元素
    int left,right,mid,NoFound = -1;
    //初始左边界
    left = 1;
    //初始右边界
    right = tb->Length;
    while(left<=right){
        //计算中间元素坐标
        mid = (left+right)/2;
        if(k<tb->Data[mid]){
            //调整右边界
            right = mid-1;
        }else if(k>tb->Data[mid]){
            //调整左边界
            left = mid+1;
        }else{
            return mid;
        }
        return NoFound;
    }
}